<template>
<div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div style="display: flex; justify-content: space-between; align-items: center;">
          <span>
            <span style="font-weight: 700; color:#606266; margin-bottom: 20px;margin-left: 20px;">关键字</span>
            <el-input v-model="params.keyword" placeholder="根据编号搜索" size="small" style="width: 230px;margin-left: 15px; "></el-input>
          </span>
          <span>
            <el-button size="small" @click="handlerClearInput">清除</el-button>
            <el-button type="primary" size="small" @click="handlerSearchInput">搜索</el-button>
          </span>
        </div>
        <template >
          <el-alert
            style="margin-bottom: 15px; margin-top: 18px; padding: 8px 16px;"
            :title="`数据一共${pages}条`"
            type="info"
            :closable="false"
            show-icon>
          </el-alert>
        </template>
        <!-- 表格渲染 -->
        <el-table
          type="info"
          effect="dark"
          :data="items"
          style="width: 100%"
          :header-cell-style="{
            background:'#F8F8F9'}">
>
            <el-table-column
              prop="id"
              label="编号"
              width="190">
            </el-table-column>
            <el-table-column
              prop="questionType"
              label="题型"
              width="80">
              <template #default="{ row }">
                {{ employeeEnum[row.questionType] }}
              </template>
            </el-table-column>
            <el-table-column
              label="题目编号"
              width="190px" >
                <template slot-scope="scope" >
                  <a href="#"
                    v-for="item in scope.row.questionIDs"
                    :key="item.index"
                    style="color:rgb(0, 153, 255);"
                    type="text"
                    @click="dialogVisible = true"
                    >
                    <i @click="showDialog(item.id)" >{{item.number}}</i>
                  </a>
                </template>
            </el-table-column>
            <el-table-column
              prop="addTime"
              label="录入时间">
            </el-table-column>
            <el-table-column
              prop="totalSeconds"
              label="答题时间(s)">
            </el-table-column>
            <el-table-column
              prop="accuracyRate"
              label="正确率(%)">
            </el-table-column>
            <el-table-column
              prop="userName"
              label="录入人">
            </el-table-column>
            <el-table-column
              fixed="right"
              label="操作"
              width="80">
            <template #default="{ row }">
              <el-button type="danger" icon="el-icon-delete" circle plain @click="removeRandoms(row.id)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
          <div class="block">
            <el-pagination type="flex" justify="end" align="right" style="height: 60px;"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="params.page"
              :page-sizes="[10, 20, 30, 50,100]"
              :page-size="params.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pages">
            </el-pagination>
          </div>
       </el-card>
       <el-dialog
          title="题目预览"
          :visible.sync="dialogVisible"
          width="50%"
          @close="closeDialog">
          <el-form :data="roleForm">
            <el-row class="text">
              <el-col :span="6">
                <span v-if="roleForm.questionType === '1'">【题型】：单选题</span>
                <span v-else-if="roleForm.questionType === '2'">【题型】：多选题</span>
                <span v-else>【题型】：简答题</span>
              </el-col>
              <el-col :span="6">
                <span> 【编号】：{{ roleForm.id }}</span>
              </el-col>
              <el-col :span="6">
                <span v-if="roleForm.difficulty === '1'">【难度】：简单</span>
                <span v-else-if="roleForm.difficulty === '2'">【难度】：一般</span>
                <span v-else>【难度】：困难</span>
              </el-col>
              <el-col :span="6">
                <span> 【标签】：{{ roleForm.tags }}</span>
              </el-col>
              <el-col :span="6">
                <span> 【学科】：{{ roleForm.subjectName }}</span>
              </el-col>
              <el-col :span="6">
                <span> 【目录】：{{ roleForm.directoryName }}</span>
              </el-col>
              <el-col :span="6">
                <span> 【方向】：{{ roleForm.direction }}</span>
              </el-col>
            </el-row>
            <hr />
            <!-- 题干部分 -->
            <el-row>
              <el-col>
                <span>【题干】：</span>
              </el-col>
            </el-row>
            <el-row>
              <el-col style="color:blue;">
                <div v-html="roleForm.question"></div>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <span>
                  <span v-if="roleForm.questionType === '1'">单选题</span>
                  <span v-else-if="roleForm.questionType === '2'">多选题</span>
                  <span v-else>简答题</span>选项：（以下选中的选项为正确答案）
                </span>
              </el-col>
            </el-row>
            <!-- 单选框 -->
            <!-- data中的数据 radio: 1  由于单选框只有一个isRight 判断相等 按钮选中-->
            <el-radio-group v-model="radio" v-if="roleForm.questionType === '1'" @change="change">
              <el-row v-for="(item, index) in roleForm.options" :key="index" style="margin:10px 0px">
                <el-radio :label="item.isRight">{{ item.title }}</el-radio>
              </el-row>
            </el-radio-group>
            <!-- 多选框 -->
            <div v-if="roleForm.questionType === '2'">
              <el-row v-for="(item, index) in options" :key="index" style="margin:10px 0px">
                <el-checkbox v-model="item.isRight" :disabled="item.isRight === 0" @change="testChange(item)"> {{ item.title }} </el-checkbox>
              </el-row>
            </div>
            <hr />
            【参考答案】：<el-button type="danger" @click="isVideoShow = true">视频答案预览</el-button>
            <el-row style="margin-top:20px;">
              <video :src="roleForm.videoURL" controls="controls" style="height: 200px; width:300px;">
                <meta name="referrer" content="no-referrer">
              </video>
            </el-row>
            <hr />
            <el-row class="answer">
              <span>【答案解析】：{{roleForm.remarks}}</span>
            </el-row>

            <hr />
            <el-row class="answer">
              <span>
                【题目备注】：{{roleForm.remarks}}
              </span>
            </el-row>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
          </span>
       </el-dialog>
    </div>
</div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'

export default {
  data () {
    return {
      // 表格数据
      items: [],
      // 分页参数
      params: {
        page: 1,
        pagesize: 20,
        // 关键字值
        keyword: ''
      },
      // 总条数
      pages: 0,
      // 枚举题型
      employeeEnum: {
        1: '单选',
        2: '多选',
        3: '简答'
      },
      // 题目预览
      dialogVisible: false,
      roleForm: {},
      radio: 1
    }
  },
  created () {
    this.randoms()
  },
  methods: {
    // 清空搜索框
    handlerClearInput () {
      this.params.keyword = ''
      this.params.page = 1
      this.randoms()
    },
    // 搜索
    handlerSearchInput () {
      this.params.page = 1
      this.randoms()
    },
    // 渲染列表
    async randoms () {
      const { data } = await randoms(this.params)
      // console.log(items)
      this.items = data.items
      this.pages = data.counts
    },
    // 分页器
    handleSizeChange (pagesize) {
      // console.log(`每页 ${pagesize} 条`)
      this.params.pagesize = pagesize
      this.randoms()
    },
    handleCurrentChange (newPage) {
      // console.log(`当前页: ${newpage}`)
      // 将页码赋值当前的最新页码
      this.params.page = newPage
      // 获取最新的表格数据
      this.randoms()
    },
    // 删除
    removeRandoms (id) {
      // 询问用户
      this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发起删除请求
        await removeRandoms({ id })
        // 提示用户
        this.$message.success('删除成功')
        // 如果当前页只剩最后一条数据，删除最后一条数据时，需要让页码值减一
        if (this.items.length === 1 && this.params.page > 1) {
          this.params.page--
        }
        // 刷新表格数据
        this.randoms()
      })
    },

    // 题目预览
    closeDialog () {
      this.dialogVisible = false
    },
    async showDialog (id) {
      this.dialogVisible = true
      const res = await detail({ id })
      console.log(res)
      this.roleForm = res.data
    },
    testChange (item) {
      if (item.isRight) {
        console.log(item.isRight)
      } else {
        item.isRight = true
      }
    },
    change () {
      this.radio = 1
    },
    close () {
      this.$emit('close')
    }
  },
  computed: {
    options () {
      // 先判断是不是多选题
      if (this.roleForm.questionType === '2') {
        if (!this.roleForm.options) {
          return // 如果没有题目选项停止后续操作
        }
        // 遍历传递过来的题目选项 如果isRight===1 代表该选项是被选中的选项
        this.roleForm.options.forEach(element => {
          if (element.isRight === 1) {
            // 将这一项的isRight改为true 多选框就会默认选择
            element.isRight = true
          }
        })
        // 修改完成之后
        return this.roleForm.options
      } else {
        // 如果不是单选题 原路返回传递过来的题目项
        return this.roleForm.options
      }
    }
  }
}
</script>

<style scoped>
</style>
